package com.example.order.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.order.entity.OrderEntity;
import com.example.order.entity.OrderProductEntity;
import com.example.order.enums.OrderStatusEnum;
import com.example.order.mapper.OrderMapper;
import com.example.order.mapper.OrderProductMapper;
import com.example.order.param.OrderParam;
import com.example.order.service.IOrderProductService;
import com.example.order.service.IOrderService;
import com.example.order.vo.OrderResultVO;
import com.example.product.feign.RemoteProductService;
import com.example.product.feign.param.Price;
import org.springframework.cloud.commons.util.IdUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements IOrderService {


    @Resource
    private RemoteProductService productService;

    @Resource
    private IOrderProductService orderProductService;


    @Override
    public OrderResultVO order(List<OrderParam> orderParam) {

        List<OrderParam> lock = new ArrayList<>();
        for (OrderParam param : orderParam) {
            Price price = new Price();
            price.setCount(param.getCount());
            price.setProductId(param.getProductId());
            Boolean tryLock = productService.lockStock(price).getData();
            if(tryLock){
                lock.add(param);
            }
        }
        if(lock.size()==0){
            throw new RuntimeException("所有商品已售寝");
        }

        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderNo(IdUtil.getSnowflakeNextIdStr());
        orderEntity.setStatus(OrderStatusEnum.PENDING_PAYMENT.getStatus());

        //构造rpc入参,远程调用
        List<Price> param = new ArrayList<>();
        for (OrderParam orderParam1 : lock) {
            Price price = new Price();
            price.setCount(orderParam1.getCount());
            price.setProductId(orderParam1.getProductId());
            param.add(price);
        }
        Integer money = productService.price(param).getData();
        orderEntity.setAmount(money);
        //插入订单表
        this.baseMapper.insert(orderEntity);
        //todo 订单关联表
        List<OrderProductEntity> orderProductEntityList = new ArrayList<>();
        for (OrderParam orderParam1 : lock) {
            OrderProductEntity orderProductEntity = new OrderProductEntity();
            orderProductEntity.setOrderId(orderEntity.getId());
            orderProductEntity.setProductId(orderParam1.getProductId());
            orderProductEntity.setNum(orderParam1.getCount());
            orderProductEntityList.add(orderProductEntity);
        }
        orderProductService.saveBatch(orderProductEntityList);
        //构造返回体
        OrderResultVO orderResultVO = new OrderResultVO();
        orderResultVO.setOrderId(orderEntity.getId());
        orderResultVO.setPayMoney(orderEntity.getAmount());
        return orderResultVO;
    }
}
